﻿$(function () {
    // 自定义验证
    var vm = new Vue({
        el: '#app',
        data() {
            return {
                countdown: 0,
                number: null,
                timer: null,
                appoints: [],
                date: null,
                device: null,
                patient: {},
                checkInInfo: {
                     
                },
                dialogVisible: false,
                inputNumber: null,
                checkInRes: false,
                dialogTimer:false,
                resMsg:null
            }
        },
        watch: {
            inputNumber: function (value) {
                if (value && vm.number !== value) {
                    vm.number = value;
                    vm.dialogVisible = false;
                    if (value) {
                        vm.loadAppointInfo();
                    }
                }
            }
        },
        methods: {
            // 倒计时刷新
            refreshCountdown() {
                const vm = this;
                if (vm.countdown !== 0) {
                    vm.countdown -= 1;
                } else {
                    window.location.href = `/Appointments/SelfServiceTerminal/Index`
                }
            },
            // 初始化倒计时
            countdownInit() {
                const vm = this;
                vm.timer= setInterval(vm.refreshCountdown, 1000);
            },
            // 重置倒计时
            reSetTimer(seconds) {
                const vm = this;
                if (vm.timer) {
                    clearInterval(vm.timer);
                }
                vm.countdown = seconds;
                vm.countdownInit();
            },
            removeTimer() {
                const vm = this;
                if (vm.timer) {
                    clearInterval(vm.timer);
                }
            },
            initInput() {
                const vm = this;
                vm.$refs.input.focus();
                // 始终保持聚焦
                vm.$refs.input.addEventListener('blur', () => {
                    setTimeout(() => {
                        vm.$refs.input.focus();
                    }, 0);
                })
            },
           
            // 加载预约信息
            loadAppointInfo() {
                const vm = this;
                vm.inputNumber = null;
                vm.removeTimer();
                axios.post(`/api/self-service/get-appointment-info`, {
                    numbers: vm.number,
                    depCodes: localStorage.getItem('depCodes')
                })
                    .then(res => {
                        var data = res.data.data;
                        if (res.data.code === 0) {
                            vm.appoints = data.appoints;
                            vm.patient = data.patient;
                            if (data.autoCheckInRes) { // 签到成功
                                if (data.autoCheckInRes.succeed) {
                                    vm.checkInRes = true;
                                    vm.resMsg = '签到成功，正在打印小票....';
                                    vm.dialogVisible = true;
                                    vm.dialogTimer = false;
                                    setTimeout(() => {
                                        vm.checkInRes = false;
                                        vm.resMsg = null;
                                        vm.dialogVisible = false;
                                        vm.reSetTimer(45);
                                    }, 8000);
                                    // 打印小票逻辑
                                    // data.autoCheckInRes.TemplateJsonStr
                                    // data.autoCheckInRes.QueueData
                                    // 使用以上二者进行打印

                                } else { // 科室设备类型下没有打印模板 或者 远程签到失败
                                    vm.checkInRes = false;
                                    vm.resMsg = data.error;
                                    vm.dialogVisible = true;
                                    vm.dialogTimer = false;
                                    vm.reSetTimer(45)
                                    setTimeout(() => {
                                        vm.resMsg = null;
                                        vm.dialogVisible = false;
                                    }, 8000)

                                }
                            }
                            else {
                                vm.reSetTimer(20);
                            }
                        } else { // 无患者预约信息 或者  无可签到/已签到预约
                            vm.checkInRes = false;
                            vm.dialogVisible = true;
                            vm.dialogTimer = true;
                            vm.resMsg = res.data.err;
                            vm.reSetTimer(10);
                        }
                    });
            },

            checkIn(appointNumber) {
                debugger;
                const vm = this;
                return new Promise(resolve => {
                    axios.post('/api/self-service/check-in', {
                        numbers: appointNumber,
                        depCodes: localStorage.getItem('depCodes')
                    }).then(res => {
                        if (res.data.code == 0) {
                            vm.checkInRes = true;
                            vm.resMsg = '签到成功，正在打印小票';
                            vm.dialogVisible = true;
                            vm.dialogTimer = false;
                            for (var i = 0; i < vm.appoints.length; i++) {
                                if (vm.appoints[i].number === appointNumber) {
                                    vm.appoints[i].status = 2;
                                }
                            }
                            vm.appoints == [...vm.appoints];
                            setTimeout(() => {
                                vm.checkInRes = false;
                                vm.resMsg = null;
                                vm.dialogVisible = false;
                                vm.dialogTimer = false;
                                vm.reSetTimer(30);
                            }, 8000);
                            
                            // 签到成功直接进行打印
                            // data.autoCheckInRes.TemplateJsonStr
                            // data.autoCheckInRes.QueueData
                            // 使用以上二者进行打印
                        } else {
                            vm.dialogVisible = true;
                            vm.dialogTimer = false;
                            vm.checkInRes = false;
                            vm.resMsg = res.data.err;
                            vm.reSetTimer(30);
                            setTimeout(() => {
                                vm.dialogVisible = false;
                                
                            }, 8000);
                        }
                        resolve();
                    })
                })
               
            },

            // 重新打印
            rePrint(appointNumber) {
                const vm = this;
                axios.get(`/api/self-service/reprint-check-in?appointNumber=${appointNumber}`)
                    .then(res => {
                        let data = res.data.data;
                        if (res.data.code === 0) {
                            // 使用 data.data 预约小票数据，获取打印模板，进行重新打印
                            // data.data 中包含 "签到小票对队列信息" 和 "打印模板Json" 
                        } else {
                            vm.checkInRes = false;
                            vm.dialogVisible = true;
                            vm.resMsg = res.data.err;
                            vm.reSetTimer(30);
                            setTimeout(() => {

                            },8000)
                        }
                    })
            }
        },
        created() {
            const vm = this;
        },
        mounted() {
            const vm = this;
            vm.initInput();
            vm.number = window.utils.getQuery('number');
            vm.loadAppointInfo();
        }
    });
});